package com.dingwen.treasure.easyexcel.util;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.dingwen.treasure.base.util.FileUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

/**
 * 　ExcelUtil
 * 　@author dingwen
 * 　@date 2022/8/2
 */
public class ExcelUtil {
    /**
     * 读
     *
     * @param fullFileName 完整文件名称
     * @param consumer     读取数据只有的消费函数
     * @param clazz        clazz
     * @return {@link List}<{@link T}>
     */
    public static <T> List<T> read(String fullFileName, Consumer<List<T>> consumer, Class clazz) {
        List<T> readData = read(fullFileName, clazz);
        // 数据消费
        consumer.accept(readData);
        return readData;
    }

    /**
     * 读
     *
     * @param fullFileName 完整文件名称
     * @param clazz        clazz
     * @return {@link List}<{@link T}>
     */
    public static <T> List<T> read(String fullFileName, Class clazz) {
        List<T> readData = new ArrayList<>(100);
        String rootPullClassPath = FileUtil.getRootPullClassPath();
        EasyExcel.read(
                rootPullClassPath.concat(fullFileName),
                clazz,
                new PageReadListener<T>(readData::addAll)
        ).sheet().doRead();
        return readData;
    }
}
